#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#include <unistd.h>
#define MAX_DIR_DEPTH 256
ino_t get_ino_byname(char *filename)
{
    struct stat file_stat;
    if (0 != stat(filename, &file_stat)) {
        perror("stat");
        exit(-1);
    }
    return file_stat.st_ino;
}

/*根据inode-number ，在当前目录中查找对应的文件名*/
char* find_name_byino(ino_t ino)
{
    DIR *dp = NULL;
    struct dirent *dptr = NULL;
    char *filename = NULL;
    if (NULL == (dp = opendir("."))) {
        fprintf(stderr, "Can not open Current Directory\n");
        exit(-1);
    } else {
        while (NULL != (dptr = readdir(dp))) {
            if (dptr->d_ino == ino) {
                filename = strdup(dptr->d_name);
                break;
            }
        }
        closedir(dp);
    }
    return filename;
}



int main(int argc, char *argv[])
{
    int i = current_depth-1;
    char *dir_stack[MAX_DIR_DEPTH];
    unsigned current_depth = 0;
    while(current_ino != parent_ino){
        ino_t current_ino = get_ino_byname(".");
        ino_t parent_ino = get_ino_byname(".."); 
        chdir("..");
        }
    dir_stack[current_depth++] = find_name_byino(current_ino);
    for (i = current_depth-1; i>=0; i--) {
       fprintf(stdout, "/%s", dir_stack[i]);
    }
    fprintf(stdout, "%s\n", current_depth==0?"/":"");
    return 0;
}
